﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using public_dbOpt;

namespace prjEMR22_154
{
    public partial class MR_contrl_exdit_out : Form
    {
        dbOPT dataOpt = new dbOPT();
        private MR_contrl_exdit _MR_contrl_exdit;

        public MR_contrl_exdit_out(MR_contrl_exdit mr_contrl_exdit)
        {
            InitializeComponent();
            _MR_contrl_exdit  = mr_contrl_exdit;
        }


        private void MR_contrl_exdit_out_Load(object sender, EventArgs e)//加载初始值
        {
            var controlTexts = _MR_contrl_exdit.GetControlTexts();  
            foreach (Control control in this.Controls)  
            {  
                if (controlTexts.ContainsKey(control.Name) && control is TextBox )  
                {  
                    control.Text = controlTexts[control.Name];  
                }  
            }
        }


        private void Save_Click_1(object sender, EventArgs e)
        {
            if (TemplateID.Text != "")
            {
                new_check();
            }
            else
            {
                MessageBox.Show("必填项有空项，请重新输入");
            }
        }
        private void new_check()
        {
            string sql = String.Format("select * from templateinfo where TemplateID ='{0}'", TemplateID.Text);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables[0].Rows.Count != 0)
            {
                // 处理用户存在的情况  
                MessageBox.Show("模板编号已存在。");
            }
            else
            {
                sql = String.Format("INSERT INTO templateinfo (TemplateID) VALUES ('{0}')", TemplateID.Text);
                dataOpt.OperateData(sql);
                foreach (Control ctrl in this.Controls)
                {
                    if (ctrl is TextBox)
                    {
                        TextBox textbox = (TextBox)ctrl;
                        string text_name = textbox.Name;
                        sql = String.Format("UPDATE templateinfo SET {0} = '{1}' where TemplateID = '{2}'", text_name, textbox.Text, TemplateID.Text);   //set后跟的属性不能加''
                        dataOpt.OperateData(sql);
                    }

                    if (ctrl is ComboBox)
                    {
                        ComboBox textbox = (ComboBox)ctrl;
                        sql = String.Format("UPDATE templateinfo SET {0} = '{1}' where TemplateID = '{2}'", textbox.Name, textbox.Text, TemplateID.Text);
                        dataOpt.OperateData(sql);
                    }
                }
                MessageBox.Show("保存成功");
            }
        }

        private void OpenExdit_Click(object sender, EventArgs e)
        {
            OpenBoxes(this);
            label18.Text = "编辑模式";
        }

        private void CencelExdit_Click(object sender, EventArgs e)
        {
            CloseBoxes(this);
            label18.Text = "只读模式";
        }
        private void CloseBoxes(Control parent)  //关闭编辑模式函数
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    textBox.ReadOnly = true;
                }

                // 递归遍历子控件  
                if (ctrl.Controls.Count > 0)
                {
                    CloseBoxes(ctrl);
                }
            }

        }

        private void OpenBoxes(Control parent)  //打开编辑模式函数
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    textBox.ReadOnly = false;
                }

                // 递归遍历子控件  
                if (ctrl.Controls.Count > 0)
                {
                    OpenBoxes(ctrl);
                }
            }

        }

        private void Cencel_Click(object sender, EventArgs e)
        {
            var controlTexts = _MR_contrl_exdit.GetControlTexts();
            foreach (Control control in this.Controls)
            {
                if (controlTexts.ContainsKey(control.Name) && control is TextBox)
                {
                    control.Text = controlTexts[control.Name];
                }
            }
        }
    }
}
